package com.itheima.reggie.common;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;

import java.math.BigInteger;
import java.util.Date;

import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;

/*
* 对象映射器，基于Jackson将Java对象转换为Json，或者将json转为Java对象
* 将json解析为Java对象的过程称为：从json反序列化为Java对象
* 从Java对象转为json的过程称为将Java对象序列化到json
* */
public class JacksonObjectMapper extends ObjectMapper {

    public JacksonObjectMapper(){
        super();
        //收到未知属性时不报异常
        this.configure(FAIL_ON_UNKNOWN_PROPERTIES,false);

        //进行反序列化时，对属性不兼容情况做处理
        this.getDeserializationConfig().withoutFeatures(FAIL_ON_UNKNOWN_PROPERTIES);


        //自定义转换规则
        SimpleModule simpleModule = new SimpleModule()
                .addSerializer(BigInteger.class, ToStringSerializer.instance)  //BigInteger转为String
                .addSerializer(Long.class,ToStringSerializer.instance)
                /*.addSerializer(Date.class,ToStringSerializer.instance)*/;//long转为String
        this.registerModule(simpleModule);
    }
}
